package com.ruoyi.caiwu.controller1;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.*;
import com.alipay.api.response.AlipayTradeRefundResponse;

import com.ruoyi.caiwu.domain.AliPayConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;


/**
 * 作者: LCG
 * 日期: 2019/10/24 15:02
 * 描述: 这里边的微信支付是一个扫二维码支付的案例
 */
@RestController
public class PayController1 {

    //电脑PC端的支付实例
    @RequestMapping(value = "pcPay", produces = "text/html; charset=UTF-8")
    public String testPay() throws AlipayApiException {


        //构建客户端
        AlipayClient client = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do"
                ,"2021000117696629",
                AliPayConfig.APP_PRIVATE_KEY,
                "JSON",
                "utf-8",
                AliPayConfig.ALIPAY_PUBLIC_KEY,
                "RSA2");

        //构建请求对象
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();

        //支付完毕后 设置回调地址（就是支付完毕后跳转的地址）
        alipayRequest.setReturnUrl("https://www.baidu.com");
              //通知地址
        alipayRequest.setNotifyUrl("https://www.tmall.com");


        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();

        //这个是自己的创建的订单号（将来每一单支付都要动态的创建一个订单号）
        model.setOutTradeNo("20150320010101004"+new Random().nextInt(10000));

        //支付宝的支付产品类型
        model.setProductCode("FAST_INSTANT_TRADE_PAY");//电脑PC端的支付

        //这个是支付的金额，传进来订单的金额
        model.setTotalAmount("88");
        //商品信息
        model.setSubject("商品10");
        model.setBody("Body");

        //设置业务参数
        alipayRequest.setBizModel(model);


        String form="";
        try {
            //发起请求
            form = client.pageExecute(alipayRequest).getBody();
            System.out.println(form);
        } catch (AlipayApiException e) {
            e.getErrMsg();
        }
        return form;

    }

    //支付查询
    @RequestMapping(value = "query")
    public String queryList()  {

        //构建客户端
        AlipayClient client = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do"
                ,"2016101300677236",
                AliPayConfig.APP_PRIVATE_KEY,
                "JSON",
                "utf-8",
                AliPayConfig.ALIPAY_PUBLIC_KEY,
                "RSA2");

        //设置请求参数
        AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

        //商户订单号，商户网站订单系统中唯一订单号
        String out_trade_no = "20150320010101004";

        //支付宝交易号这个号码是由支付宝帮我们生成的
        String trade_no = "";

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"}");

        //请求
        String result = "eror";
        try {
            result = client.execute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        Map<String,Object> map=new HashMap<String,Object>();
        Map map1 = JSON.parseObject(result, map.getClass());
        System.out.println(JSON.toJSONString(map1.get("alipay_trade_query_response")));
        return result;
    }

    //PC交易退款
    @RequestMapping(value = "tuiKuan" )
    public String jiaoyiReturn() throws AlipayApiException {

        //构建客户端
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do"
                ,"2016101300677236",
                AliPayConfig.APP_PRIVATE_KEY,
                "JSON",
                "utf-8",
                AliPayConfig.ALIPAY_PUBLIC_KEY,
                "RSA2");

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
        request.setBizContent("{" +
                "\"out_trade_no\":\"20150320010101004\"," +
                "\"refund_amount\":88" +
                "  }");
        AlipayTradeRefundResponse response = alipayClient.execute(request);
        if(response.isSuccess()){
            System.out.println("调用成功--->"+response.getBody());
        } else {
            System.out.println("调用失败");
        }
         return response.getBody();
    }

    //退款查询
    @RequestMapping(value = "tuiKuanChaXun")
    public String tuiKuanChaXun(){



        return null;
    }

}
